using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controls.Actions.Support;

namespace Sedna.Core.Controls.Grid
{
    public class GridPopupButton : EnterPopupButton, IDataGridAwareControl
    {
        private IDataGridController controller;

        public IDataGridController Controller
        {
            set { controller = value; }
        }
        
        
        
        public GridPopupButton(IConfiguration configuration) : base(configuration)
        {
        }

        protected override void OnValueChanged(EventArgs e)
        {
            UpdateEntity();
            base.OnValueChanged(e);
        }

        private void UpdateEntity()
        {
            object entity = this.controller.GridSourceCache[(int)ViewState["entityIndex"]];

            if (binder.Unbind(entity, "Value", Configuration.DataParser))
            {
                controller.TransientData.Add(entity);
            }
        }


        public void OnDataBinding(DataGridItem dataGridItem)
        {
            ViewState["entityIndex"] = dataGridItem.DataSetIndex;
            base.Bind();
            binder.Bind(dataGridItem.DataItem, "Value", Configuration.DataTranslator);
        }


        protected override void Bind()
        {
        }

        protected override void Unbind()
        {
        }
    }
}
